package com.djx.监听;

import com.djx.基础.MyZookepper01;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class getChildrenWatcher {

    ZooKeeper zooKeeper = new MyZookepper01().zooKeeper;

    @Test
    public void getChildren() throws IOException, KeeperException, InterruptedException {
//        zooKeeper.addAuthInfo("digest","super:admin".getBytes());
        zooKeeper.getChildren("/watcher3", true);

        System.out.println("-0-");

        Thread.sleep(50000);
        System.out.println("结束!");

    }


    @Test
    public void getChildrenOne() throws IOException, KeeperException, InterruptedException {
//        zooKeeper.addAuthInfo("digest","super:admin".getBytes());
        zooKeeper.getChildren("/watcher3", new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                System.out.println(watchedEvent.getPath());
                System.out.println(watchedEvent.getType());
                if (watchedEvent.getType() == Event.EventType.NodeChildrenChanged){
                    try {
                        zooKeeper.getChildren("/watcher3",this);
                    } catch (KeeperException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        });

        System.out.println("-0-");

        Thread.sleep(50000);
        System.out.println("结束!");

    }
}
